﻿using StockholmsStad.Data.Entity;

namespace StockholmsStad.Data.ContractFactory
{
    /// <summary>
    /// Konvertering befintligt dataobjekt till kontraktsobjekt
    /// </summary>
    internal class AddressFactory
    {
        public Address CreateEntityObject(Runtime.Serialization.DataContract.Address address)
        {
            if (address == null)
                return null;
            var contractAddress = new Address
                                      {
                                          City = address.City,
                                          StreetAddress = address.StreetAddress,
                                          PostCode = address.PostCode
                                      };

            if (address.CoAddress != null)
                contractAddress.CareOf = address.CoAddress;

            if (address.Country != null)
                contractAddress.Country = address.Country;

            return contractAddress;
        }

        public Runtime.Serialization.DataContract.Address CreateContractObject(Address address)
        {
            var entityAddress = new Runtime.Serialization.DataContract.Address
                                    {
                                        StreetAddress = address.StreetAddress,
                                        PostCode = address.PostCode,
                                        City = address.City
                                    };

            if (address.CareOf != null)
                entityAddress.CoAddress = address.CareOf;

            if (address.Country != null)
                entityAddress.Country = address.Country;

            return entityAddress;
        }

        public void UpdateEntityObject(Runtime.Serialization.DataContract.Address address, Address addressEntity)
        {
            addressEntity.CareOf = address.CoAddress;
            addressEntity.City = address.City;
            addressEntity.Country = address.Country;
            addressEntity.PostCode = address.PostCode;
            addressEntity.StreetAddress = address.StreetAddress;
        }
    }
}